import {defineStore} from 'pinia'
import {brandDate, reqData,editBrandData} from "@/api/brand/type.ts";
import { getBrandList } from "@/api/brand";

interface option {
  name: string
}
export const useBrandStore = defineStore('brand', () => {
    const regionOptions = reactive<option[]>([])
    const categoryOptions = reactive<option[]>([])
    const brandList: brandDate[] = reactive([])
    const pageSize = reactive([10, 20, 30, 40, 50])
    const total = ref(0)
    const reqFrom: reqData = reactive({
        name: '',
        region: '',
        category: '',
        status: 1,
        page: 1,
        pageSize: pageSize[0]
    })
    const addBrand = ref(false)
    const editBrand = ref(false)
    const editBrandData = reactive<editBrandData>({
        ID: -1,
        name: '',
        logo: '',
        description: '',
        region: '',
        category: '',
        status: -1
    })
    // 获取品牌列表
    const getBrandListFunc = async ():Promise<string> => {
        const res = await getBrandList(reqFrom)
        if (res.code === 200) {
            brandList.length = 0
            brandList.push(...res.data.brandList)
            total.value = res.data.total
            return Promise.resolve("ok")
        } else {
            return Promise.reject(res.msg)
        }
    }

    return {
        brandList,
        reqFrom,
        pageSize,
        total,
        regionOptions,
        categoryOptions,
        getBrandListFunc,
        addBrand,
        editBrand,
        editBrandData
    }
})